// #include "kvstore.h"
#include "kvs.h"

int kvs_hash_create(hash_t **ht, size_t bucket_count)
{
    if (!ht)
        return -1;
    if (*ht)
        return -1;
    *ht = hashtable_create(bucket_count);
    if (!ht)
        return -1;

#if (FULL_PERSIST && INIT_BY_FULL)
    LOG("正在同步哈希表全量持久化: %s\n", HASH_FULL_FILE_NAME);
    int ret = hashtable_full_restore(HASH_FULL_FILE_NAME, *ht);
    if (ret == -1)
    {
        LOG("哈希表全量持久化失败, 建立空的哈希表\n");
    }
    else
    {
        LOG("哈希表全量持久化成功\n");
    }
#endif
    return 0;
}

int kvs_hash_destroy(hash_t *ht)
{
#if (FULL_PERSIST && STORE_FULL)
    // 全量持久化到文件
#if BACK_UP_OLD_FULL_PERSIST_FILE
    if (hashtable_full_store_with_backup(ht, HASH_FULL_FILE_NAME) == 0)
    {
        printf("哈希表全量文件保存成功\n");
    }
#else
    if (hashtable_full_store(ht, HASH_FULL_FILE_NAME) == 0)
    {
        printf("哈希表全量文件保存成功\n");
    }
#endif
#endif
    hashtable_destroy(ht);
}


int kvs_hash_set(hash_t *ht, char *key, char *value)
{
    if (ht == NULL)
    {
        LOG("ht: NULL\n");
    }

    if (!ht || !key || !value)
        return -1;
    int ret = hashtable_insert(ht, key, value);
    if (ret == 1)
    {
        return 1;
    }
    if (ret == 0)
    {
        #if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "HSET", key, value);
        if (res == 0)
        {
            // LOG("[AOF]: HSET ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: HSET ASYN FAIL\n");
        }
#endif

        return 0;
    }
    if (ret == -1)
    {
        return -1;
    }
}

char *kvs_hash_get(hash_t *ht, char *key)
{
    if (!ht || !key)
        return NULL;

    char *buf = hashtable_find(ht, key);
    if (buf == NULL)
    {
        return NULL;
    }
    return buf;
}

int kvs_hash_del(hash_t *ht, char *key)
{
    if (!ht || !key)
        return -1;

    int ret = hashtable_delete(ht, key);
    if (ret == 1)
    {
        return 1;
    }
    if (ret == 0)
    {
        #if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "HDEL", key, NULL);
        if (res == 0)
        {
            // LOG("[AOF]: HDEL ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: HDEL ASYN FAIL\n");
        }
#endif

        return 0;
    }
}

int kvs_hash_all_del(hash_t *ht)
{
    if (!ht)
        return -1;

    int ret = hashtable_clear(ht);
    if (ret == -1)
    {
        return -1;
    }
    if (ret == 0)
    {
        #if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "HDELA", NULL, NULL);
        if (res == 0)
        {
            // LOG("[AOF]: HDELA ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: HDELA ASYN FAIL\n");
        }
#endif
        return 0;
    }
}

int kvs_hash_mod(hash_t *ht, char *key, char *value)
{

    if (!ht || !key || !value)
        return -1;

    int ret = hashtable_update(ht, key, value);
    if (ret == 1)
    {
        return 1;
    }
    if (ret == -1)
    {
        return -1;
    }
    if (ret == 0)
    {
        #if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "HMOD", key, value);
        if (res == 0)
        {
            // LOG("[AOF]: HMOD ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: HMOD ASYN FAIL\n");
        }
#endif
        return 0;
    }
}

int kvs_hash_exist(hash_t *ht, char *key)
{
    if (!ht || !key)
        return -1;

    char *buf = hashtable_find(ht, key);
    if (buf == NULL)
        return 1;
    return 0;
}

int kvs_hash_count(hash_t *ht)
{
    if (ht == NULL)
        return -1;

    int count = hashtable_count(ht);
    return count;
}